const CompressionPlugin = require('compression-webpack-plugin'),
      productionGzipExtensions = [
        'js',
        'css',
        'svg',
        'woff',
        'ttf',
        'json',
        'html',
      ],
      path = require('path')

const resolve = dir => path.join(__dirname, dir)

const devConfig = {
  /* 启动配置 */
  devServer: {
    open: true,
    inline: true,
    hot: false,
    host: '192.168.1.103',
    port: 9988,
    https: false,
  },
}

const commonConfig = {
  chainWebpack: config => {
    config.resolve.alias
      .set('styles', resolve('./src/assets/css'))
      .set('image', resolve('./src/assets/img'))
      .set('apis', resolve('./src/api'))
      .set('static', resolve('./src/assets'))
      .set('views', resolve('./src/views'))
      .set('comps', resolve('./src/components'))
    config.module
      .rule('images')
      .use('url-loader')
      .loader('url-loader')
      .tap(options => Object.assign(options, {
        limit: 20000,
      }))
      .end()
    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/img/svg'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/img/svg'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]',
      })
      .end()
  },
}

const defaultConfig = {
  configureWebpack: {
    plugins: [
      // Gzip
      new CompressionPlugin({
        filename: '[path].gz[query]',
        test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
        threshold: 1024,
        minRatio: 0.8, //压缩率大于0.8的才压缩
        deleteOriginalAssets: false, //是否删除原文件
      }),
    ],
  },
  productionSourceMap: false, //不输出map文件
}

module.exports = {
  ...devConfig,
  ...commonConfig,
  ...defaultConfig,
}